﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class InvoiceDAO
    {
        public InvoiceDTO CreateInvoiceDTOFromEntity(HoaDon hd)
        {
            InvoiceDTO invoiceDto = new InvoiceDTO
            {
                InvoiceID = hd.HoaDonID,
                InvoiceCode = hd.MaHoaDon,
                CustomerID = hd.KhachHangID ?? 0,
                CreatedDate = hd.NgayLap ?? DateTime.Now,
                CreatedBy = hd.NguoiLap ?? 0,
                GrandTotal = float.Parse( hd.TongTien.ToString()),
                Description = hd.GhiChu,
                Status = bool.Parse(hd.SuDung.ToString())
            };

            using (BookStoreEntities ent = new BookStoreEntities())
            {
                invoiceDto.CustomerName = ent.KhachHangs.FirstOrDefault(k => k.KhachHangID == invoiceDto.CustomerID).HoTen;
            }

            return invoiceDto;
        }

        public int InsertInvoice(InvoiceDTO invoice)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    HoaDon hd = new HoaDon
                    {
                        MaHoaDon = invoice.InvoiceCode,
                        KhachHangID = invoice.CustomerID,
                        NgayLap = invoice.CreatedDate,
                        NguoiLap = invoice.CreatedBy,
                        TongTien = invoice.GrandTotal,
                        GhiChu = invoice.Description,
                        SuDung = invoice.Status
                    };

                    if (ent.HoaDons.Count(t => t.HoaDonID != null) > 0)
                    {
                        hd.HoaDonID = ent.HoaDons.Max(t => t.HoaDonID) + 1;
                    }
                    else
                    {
                        hd.HoaDonID = 1;
                    }

                    ent.AddToHoaDons(hd);

                    DocSequenceDAO docSeqDAO = new DocSequenceDAO();
                    docSeqDAO.UpdateNextDocSequence(DocSequence.Invoice.ToString());

                    ent.SaveChanges();

                    return hd.HoaDonID;
                }
            }
            catch
            {
                return -1;
            }
        }

        public int InsertInvoiceDetail(InvoiceDetailDTO invoiceDetail)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    ChiTietHoaDon cthd = new ChiTietHoaDon
                    {
                        HoaDonID = invoiceDetail.InvoiceID,
                        SachID = invoiceDetail.BookID,
                        SoLuong = invoiceDetail.Quantity,
                        GiaBan = invoiceDetail.UnitPrice
                    };

                    if (ent.ChiTietHoaDons.Count(t => t.ChiTietID != null) > 0)
                    {
                        cthd.ChiTietID = ent.ChiTietHoaDons.Max(t => t.ChiTietID) + 1;
                    }
                    else
                    {
                        cthd.ChiTietID = 1;
                    }

                    ent.AddToChiTietHoaDons(cthd);
                    ent.SaveChanges();

                    return cthd.ChiTietID;
                }
            }
            catch
            {
                return -1;
            }
        }

        public int GetQuantitySellForReport(int bookID, DateTime from, DateTime to)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                int quantity = 0;
                List<HoaDon> listInvoice = ent.HoaDons.Where(h => h.NgayLap >= from && h.NgayLap <= to).ToList();

                foreach (HoaDon hd in listInvoice)
                {
                    quantity += ent.ChiTietHoaDons.Where(c => c.HoaDonID == hd.HoaDonID && c.SachID == bookID).First().SoLuong ?? 0;
                }

                return quantity;
            }
        }

        public int GetTotalBuy(int customerID, int month)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int year = DateTime.Now.Year;
                    List<HoaDon> hds = ent.HoaDons.Where(c => c.KhachHangID == customerID && DateTime.Parse(c.NgayLap.ToString()).Month == month && DateTime.Parse(c.NgayLap.ToString()).Year == year).ToList();

                    int total = 0;

                    foreach (HoaDon hd in hds)
                    {
                        total += (int)hd.TongTien;
                    }

                    return total;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}
